package com.hps.tccdemo.bank2.service.impl;

import com.hps.tccdemo.bank2.dao.AccountInfoDao;
import com.hps.tccdemo.bank2.service.AccountInfoService;
import lombok.extern.slf4j.Slf4j;
import org.dromara.hmily.annotation.Hmily;
import org.dromara.hmily.core.concurrent.threadlocal.HmilyTransactionContextLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author heps
 * @date 2019/12/14 10:46
 */
@Slf4j
@Service
public class AccountInfoServiceImpl implements AccountInfoService {

    @Autowired
    private AccountInfoDao accountInfoDao;


    @Override
    @Transactional
    @Hmily(confirmMethod = "commit", cancelMethod = "rollback")
    public void updateAccountBalance(String accountNo, Double amount) {
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 try begin 开始执行，xid: {}", transId);
    }

    @Transactional
    public void commit(String accountNo, Double amount) {
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 confirm begin 开始执行，xid: {}", transId);
        if (accountInfoDao.isExistConfirm(transId) > 0) {
            log.info("bank2 confirm 已经执行，无需重复执行, xid: {}", transId);
        }
        accountInfoDao.addAccountBalance(accountNo, amount);
        accountInfoDao.addConfirm(transId);
    }

    @Transactional
    public void rollback(String accountNo, Double amount) {
        String transId = HmilyTransactionContextLocal.getInstance().get().getTransId();
        log.info("bank2 cancel begin 开始执行，xid: {}", transId);
    }
}
